<template>
    <div>
        <el-form
                v-loading="loading"
                element-loading-text="正在登入中"
                ref="loginForm" :rules="rules"
                :model="loginForm" class="loginContainer">
            <h3 class="loginTitle">系统登入</h3>
            <el-form-item label="用户名" prop="username">
                <el-input type="text" v-model="loginForm.username" auto-complete="off"
                          placeholder="请输入用户名" @keydown.enter.native="submitLogin"></el-input>
            </el-form-item>
            <el-form-item label="密码" prop="password">
                <el-input type="password" v-model="loginForm.password" auto-complete="off"
                          placeholder="请输入密码" ></el-input>
            </el-form-item>
            <el-form-item label="验证码" prop="code" >
                <div style="width: 400px;display: flex;align-items: center">
                    <el-input style="width: 250px;" v-model="loginForm.code" auto-complete="off"
                              placeholder="请输入验证码" @keydown.enter.native="submitLogin"></el-input>
                    <img style="margin-left: 20px" :src="vcUrl" alt="" @click="updateCode">
                </div>
            </el-form-item>

            <el-checkbox style="margin-top: 10px" label="记住我" v-model="checked" class="loginRemenberme"></el-checkbox>

            <el-button type="primary" style="width: 100%;" @click="submitLogin">登入</el-button>
        </el-form>
    </div>
</template>

<script>
    export default {
        name: "Login",
        data() {
            return {
                loginForm:{
                    username:'yixin',
                    password:'19951016',
                    code:''
                },
                checked:true,
                rules:{
                    username:[{required:true,message:'请输入用户名',trigger:'blur'}],
                    password:[{required:true,message:'请输入密码',trigger:'blur'}],
                    // code:[{required:true,message:'请输入验证码',trigger:'blur'}]
                },
                loading:false,
                vcUrl:'/verifyCode?time='+new Date(),
            }
        },
        methods:{
            updateCode(){
                this.vcUrl = '/verifyCode?time='+new Date();
            },
            submitLogin(){
                this.$refs.loginForm.validate((valid) => {
                    if (valid) {
                        this.loading=true;
                        this.postKeyValueRequest('/doLogin',this.loginForm).then(resp=>{
                            this.loading=false;
                            if (resp){
                                this.$store.commit('INIT_CURRENTHR',resp.obj);
                                window.sessionStorage.setItem("user",JSON.stringify(resp.obj));
                                let path = this.$route.query.redirect;
                                this.$router.replace((path == '/' || path == undefined) ? '/home':path);
                            } else {
                                this.vcUrl = '/verifyCode?time='+new Date();
                            }
                        });
                    } else {
                        return false;
                    }
                });
            }
        }
    }
</script>

<style scoped>
    .el-form-item__content{
        display: flex;
        align-items: center;
    }
    .loginContainer{
        border-radius: 15px;
        background-clip: padding-box;
        margin: 180px auto;
        width: 400px;
        padding: 35px 35px 25px 35px;
        background: #ffffff;
        border: 1px solid #eaeaea;
        box-shadow: 0 0 25px #cac6c6;
    }
    .loginTitle{
        margin: 10px auto 20px auto;
        text-align: center;
        color: #505458;
    }
    .loginRemenberme{
        padding-bottom: 10px;
    }
</style>